<template>
  <div>
    <el-dialog title="编辑分类" :visible.sync="dialogVisible" @close='dialogFormVisible' :close-on-click-modal="false"
      :close-on-press-escape="false">
      <el-form size="small" :model="form" :rules="formRules" ref="form">
        <el-form-item label="所属分类" :label-width="formLabelWidth">
          <el-select v-model="form.parent_id">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option :value="cat.category_id" :label="cat.name" :key="cat.category_id"
              v-for="cat in addform"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="保证金(元)" :label-width="formLabelWidth" prop="deposit_money" v-if="form.parent_id">
          <el-input v-model="form.deposit_money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="主营类别图片" prop="image_id" :label-width="formLabelWidth">
          <el-row>
            <el-button type="primary" @click="openUpload">选择图片</el-button>
            <div v-if="form.image_id != ''" class="img">
              <img :src="file_path" width="100" height="100" />
            </div>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible">取 消</el-button>
        <el-button type="primary" @click="editCategory" :loading="loading">确 定</el-button>
      </div>
    </el-dialog>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" @returnImgs="returnImgsFunc">上传图片</Upload>
  </div>
</template>

<script>
import Upload from '@/components/file/Upload';
import SupplierApi from '@/api/supplier.js';
export default {
  components: {
    Upload
  },
  data() {
    return {
      isupload: false,
      file_path: '',
      formRules: {
        name: [{
          required: true,
          message: '请输入分类名称',
          trigger: 'blur'
        }],
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false,
    };
  },
  props: ['open_edit', 'addform', 'form'],
  created() {
    this.dialogVisible = this.open_edit;
    console.log(this.form)
    this.file_path = this.form.image_url
  },
  methods: {
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.file_path = e[0].file_path;
        this.form.image_id = e[0].file_id;
      }
      this.isupload = false;
    },
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*修改分类*/
    editCategory() {
      let self = this;
      let params = this.form;
      self.loading = true;
      SupplierApi.editCategory(params, true)
        .then(data => {
          self.loading = false;
          self.$message({
            message: '恭喜你，修改成功',
            type: 'success'
          });
          self.dialogFormVisible(true);

        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        })
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        })
      }
    }

  }
};
</script>

<style></style>
